#！/usr/bin/python 
# -*- coding:utf-8 -*-  
# @Time: 2019/5/21 20:51   
# @Author: Envy   
# @File: send_email_test.py

# python manage.py shell
# 进入shell环境，依次执行下面4句话，就能进行邮箱发送功能的实现
# 方法1：使用send_email来实现邮箱发送
from django.core.mail import send_mail
from django.core.mail import send_mass_mail
from django.core.mail import EmailMultiAlternatives
from django.conf import settings


# 获取settings.py的配置信息
from_email = settings.DEFAULT_FROM_EMAIL
# 发送邮件，接收邮件以列表表示，说明可设置多个接收对象
# send_mail(subject, message, from_email, recipient_list,fail_silently=False, auth_user=None, auth_password=None,connection=None, html_message=None)

send_mail('MyDjango', 'This is a django', from_email, recipient_list=['2131247535@qq.com'])



# 方法2：使用send_mass_mail来实现多封邮件发送
# send_mass_mail(datatuple, fail_silently=False, auth_user=None,auth_password=None, connection=None)
message1 = ('MyDjango', 'This is a django', from_email, ['2131247535@qq.com'])
message2 = ('MyDjango', 'Hello,Django', from_email, ['2131247535@qq.com'])
message3 = ('MyDjango', 'Study Django Well', from_email, ['2131247535@qq.com'])
send_mass_mail((message1,message2,message3), fail_silently=False)


# 方法3：使用EmailMultiAlternatives实现邮件发送
# EmailMultiAlternatives(subject='', body='', from_email=None, to=None, bcc=None,connection=None, attachments=None, headers=None, alternatives=None,cc=None, reply_to=None)
content = '<p>这是一封<strong>重要</strong>的邮件。</p>'
msg = EmailMultiAlternatives('MyDjango',content, from_email, ['2131247535@qq.com'])
# 将正文设置为HTML格式
msg.content_subtype = 'html'
# attach_alternative对正文内容进行补充和帮助
msg.attach_alternative('<strong>This is from Django</strong>', 'text/html')
# 添加附件（可选）
# msg.attach_file('I:test.csv')
# 发送邮件
msg.send()